<template>
  <div class="se-container">
    <div class="se-header">商品仓库</div>
    <div class="se-body">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      </el-row>

      <!--   表格   -->
      <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="id" width="55" align="center" label="id"  />
        <el-table-column label="仓库名称" align="center" prop="warehouseName" show-overflow-tooltip/>
        <el-table-column label="创建时间" align="center" prop="createTime"          show-overflow-tooltip/>
        <el-table-column label="创建用户" align="center" prop="createBy"    show-overflow-tooltip />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!--   添加或修改   -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="仓库名称" prop="warehouseName">
            <el-input v-model="form.warehouseName" placeholder="请输入仓库名称"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { warehouseQueryList, warehouseQueryId, warehouseAdd, warehouseUpdate, warehouseDelete } from '@/api/jxc/warehouse'

  export default {
    /** 采购管理 的 商品仓库 */
    name: 'warehouseManager',
    data() {
      return {
        loading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        warehouseList: [],
        form: {},
        /** 控制弹窗 */
        open: false,
        // 弹出层标题
        title: "",
        /** 验证 */
        rules: {
          warehouseName: [
            { required: true, message: "商品仓库名称不能为空", trigger: "blur" }
          ]
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          id: undefined,
          warehouseName: undefined,
          createTime: undefined,
          createBy: undefined
        },
        /** 总数 */
        total: 0
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      getList() {
        warehouseQueryList(this.queryParams)
          .then(res => {
            this.warehouseList = res.data
            this.total = res.total
          })
      },
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        warehouseQueryId({id: id}).then(res => {
          this.form = res.data
          this.open = true
          this.title = '修改商品仓库'
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              warehouseUpdate(this.form).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  });
                  this.open = false;
                  this.getList();
                } else {
                  this.$message({
                    message: '删除失败',
                    type: 'error'
                  })
                }
              })
            } else {
              warehouseAdd(this.form).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '添加成功',
                    type: 'success'
                  })
                  this.open = false;
                  this.getList();
                } else {
                  this.$message({
                    message: '添加失败',
                    type: 'error'
                  })
                }
              })
            }
          }
        });
      },
      handleDelete(row) {
        let delIds = row.id || this.ids;

        if (delIds instanceof Array) {
          let temp = ''
          for (let i = 0; i < delIds.length; i++) {
            let e = delIds[i]
            if (i < delIds.length - 1) e += ','
            temp += e
          }
          delIds = temp
        }
        const __this = this

        this.$confirm('是否确认删除单位id为"[' + delIds + ']"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          warehouseDelete({ids: delIds}).then(res => {
            if (res.code == 0) {
              __this.$message({
                message: '删除成功',
                type: 'success'
              })
              __this.getList()
            } else {
              __this.$message({
                message: '删除失败',
                type: 'error'
              })
            }
          })
        })
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          warehouseName: undefined,
          createTime: undefined,
          createBy: undefined
        };
      },
      /** 添加 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品仓库";
      }
    }
  }
</script>

<style lang="scss" scoped>
  .se-header {
    height: 60px;
    position: relative;
    z-index: 100;
    padding: 0 20px;
    font-size: 18px;
    line-height: 60px;
  }

  .se-body {
    background-color: white;
    border: 1px solid #e6e6e6;
    border-radius: 2px;
  }

  .se-table-header {
    height: 50px;
    background-color: white;
    position: relative;
    .se-table-header-button {
      float: right;
      margin-right: 20px;
      margin-top: 10px;
    }
  }

  .project-reminder {
    width: auto;
    float: left;
    margin-left: 20px;
    margin-top: 10px;
  }
</style>
